<template>
  <div class="deliver">
    <el-form
      ref="form"
      :model="form"
      label-width="80px"
      enctype="multipart/form-data"
      action="/newslist"
    >
      <el-form-item label="标题名称">
        <el-input v-model="form.title" style="width:500px"></el-input>
      </el-form-item>
      <el-form-item label="文章类型">
        <el-select v-model="form.types" placeholder="请选择文章类型">
          <el-option label="打卡" value="打卡"></el-option>
          <el-option label="生活" value="生活"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="图片上传">
        <!-- 动态获取接口地址，所以action加冒号 -->
        <el-upload
          :multiple="true"
          class="avatar-uploader"
          :action="$http.defaults.baseURL + '/upload'"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="form.icon" :src="form.icon" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="发布时间">
        <el-col :span="11">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="form.upload_time"
            style="width: 50%;"
          ></el-date-picker>
          <el-time-picker
            type="date"
            placeholder="选择时间"
            v-model="form.upload_time"
            style="width: 50%;"
          ></el-time-picker>
        </el-col>
      </el-form-item>
      <el-form-item label="即时发布">
        <el-switch v-model="form.delivery"></el-switch>
      </el-form-item>
      <el-form-item label="文章内容">
        <el-input type="textarea" v-model="form.content"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="add()" v-if="!this.id">立即创建</el-button>
        <el-button type="primary" @click="edit()" v-else>立即修改</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// import { DeliverInfo } from '@/api/deliverInfoAPI'
import { DeliverInfo, GetNewsListById,UpdateNewsListById} from '@/api'
export default {
  props:{
    id:{}
  },
  data() {
    return {
      form: {
        title: '',
        upload_time: '',
        delivery: true,
        types: [],
        content: '',
        icon: '',
        cover: {
          type: '',
          images: [],
        },
      },
      imageUrl: '',
      picurl: '',
    }
  },
  methods: {
    async fetch(){
      const {data:res}=await GetNewsListById(this.id)
        console.log(res,'001122')
        this.form=res.data
    },
    async add() {
      const { data: res } = await DeliverInfo(this.form)
      console.log(res)
      this.form={}
    },
    async edit(){
      const {data:res}=await UpdateNewsListById(this.id,this.form)
      console.log(res,'edit')
      this.$router.push('/newslist')
    },
    handleAvatarSuccess(res) {
      //   this.imageUrl = URL.createObjectURL(file.raw);
      //   console.log(res[0].filename)
      //   console.log(file)
      //   this.model.icon=file.path
      //   console.log(this.imageUrl)
      //   this.form.images.push(this.imageUrl)
      console.log(res)
      this.$set(this.form, 'icon', res[0].url)
      // this.model.icon=res.url

      //  const files=[]
      //  files.push(file.response)
      //  const fd=new FormData()

      //  for(let i = 0; i < files.length; i++) {
      // 在这里数组每一项的.raw才是你需要的文件，有疑惑的可以打印到控制台看一下就清楚了
      // fd.append('file', files[i].url);
      // }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
  },
  created() {
    this.id && this.fetch()
    
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}
.avatar {
  width: 150px;
  height: 150px;
  display: block;
}
</style>

<style lang="less" scoped>
.deliver {
}
</style>
>
